<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<style>
    .table tbody tr td {
        vertical-align: middle;
    }

    .table .thead-blue th {
        color: #fff;
        background-color: #3c8dbc;
    }
</style>
<div class="row">
    <div class="col-sm-12">
        <div class="box">
            <div class="box-header with-border">
                <h2 class="page-header">
                    <i class="fa fa-globe"></i> <?php echo $site["name"]; ?> 询盘
                    <small class="pull-right"> <a href="<?php echo $site["domian"]; ?>"
                                                  target="_blank"><?php echo $site["domian"]; ?></a></small>

                </h2>

                <?php echo $nav; ?>

            </div>
            <div class="box-body">

                <div class="mb-10">

                    <?php echo form_open(current_url(), array('class' => 'form-inline', 'method' => 'get', 'id' => 'search_goods')); ?>

                    <div class="form-group" style="margin-right:15px;">
                        <?php echo form_input($keyword); ?>
                    </div>


                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索询盘")); ?>
                    <span class="btn btn-flat btn-info btn-sm" id="export">导出订单</span>
                    <?php echo form_close(); ?>


                </div>

                <?php if ($loglist): ?>
                    <?php echo form_open("/backend/sites/order/export", array('class' => 'form-inline', 'method' => 'post', 'id' => 'batch_goods')); ?>
                    <table class="table table-striped table-hover" style="margin-top:20px;">
                        <thead class="thead-blue">
                        <tr>
                            <th width="50">ID</th>
                            <th>真实姓名</th>
                            <th>邮箱</th>
                            <th>电话</th>
                            <th>数量</th>
                            <th width="400">留言</th>
                            <th>时间</th>
                            <th>IP地址</th>
                            <th>来源</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($loglist as $item): ?>
                            <tr itemid="<?php echo $item["id"]; ?>">
                                <td><?php echo $item["id"]; ?></td>

                                <td>
                                    <?php
                                    if (ismobile($item["agent"])) {
                                        echo '<i class="fa fa-phone-square" aria-hidden="true"></i>';
                                    } else {

                                    }
                                    ?><?php echo $item["realname"]; ?>
                                </td>
                                <td>
                                    <?php echo $item["email"]; ?>
                                </td>
                                <td>
                                    <?php echo $item["phone"]; ?>
                                </td>
                                <td>
                                    <?php echo $item["quantity"]; ?>
                                </td>
                                <td width="">
                                    <?php echo $item["info"]; ?>
                                </td>
                                <td><?php echo date("Y-m-d H:i", strtotime($item["addtime"])); ?></td>
                                <td>
                                    <a href="https://ip.tool.chinaz.com/<?php echo $item["ipaddr"]; ?>" target="_blank">查询IP</a>
                                </td>
                                <td>
                                    <?php echo $item["url"] ? '<a href="' . $item["url"] . '" target="_blank">查看<a/>' : ""; ?>
                                </td>
                                <td cid="<?php echo $item["id"]; ?>"><span
                                            ondblclick="edit(this,'note');" <?php if ($item["note"]) echo 'style="color:red;"'; ?>><?php echo $item["note"] ? htmlentities($item["note"]) : "双击添加备注"; ?></span>
                                </td>
                            </tr>

                        <?php endforeach; ?>
                        </tbody>
                    </table>
                    </form>
                <?php endif; ?>
                <?php
                if (empty($loglist)) {
                    echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                }
                ?>

                <?php echo $pager; ?>

                <div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
                    <?php echo '共' . $total . '条订单，' . $page . '/' . $pages . '页'; ?>

                    <select id="pagesize" class="form-control input-sm" style="height:22px;">
                        <option value="0">每页数量</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                        <option value="200">200</option>
                        <option value="500">500</option>
                    </select>

                    <input type="text" id="topage" value="" class="form-control input-sm" placeholder='页码'
                           style="margin:0 5px;height:22px;width:45px;">
                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-xs btn-primary btn-flat', 'id' => 'jumpbtn', 'content' => "跳转")); ?>
                </div>

            </div>
        </div>
    </div>
</div>


<script src="/assets/backend/lazyload.js?v=1.9.1"></script>
<script>

    window.addEventListener('DOMContentLoaded', function () {
        (function ($) {
            $(document).ready(function () {
                // Init iCheck
                var elem_input = $('input');
                if (elem_input.length) {
                    elem_input.iCheck({
                        checkboxClass: 'icheckbox_square-blue',
                        radioClass: 'iradio_square-blue'
                    });
                }

            });
        })(jQuery);
    });

    $(document).ready(function () {

        function initTableCheckbox() {
            var $thr = $('table thead tr');
            var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
            $thr.prepend($checkAllTh);
            $('#checkAll').on('ifChecked ifUnchecked', function (event) {
                $tbr.find('input').each(function () {
                    $(this).iCheck('toggle');
                    $(this).parent().parent().toggleClass('warning');
                });
            });
            var $tbr = $('table tbody tr');
            var $checkItemTd = $('<td><input type="checkbox" name="checkItem[]" /></td>');
            $tbr.prepend($checkItemTd);

            $tbr.find('input').on('ifChecked ifUnchecked', function (event) {
                $(this).val($(this).parent().parent().parent().attr("itemid"));
                $(this).parent().parent().parent().toggleClass('warning');
            });
            $tbr.click(function () {
                $(this).find('input').iCheck('toggle');
            });
        }

        initTableCheckbox();

        $("#batchexport").on("click", function () {

            var checkcount = 0;
            $("table tbody").find("input").each(function () {
                if ($(this).prop('checked')) {
                    checkcount = checkcount + 1;
                }
            });

            if (checkcount == 0) {
                layer.alert('没有选中任何数据');
                return;
            }

            $("form#batch_goods").submit();

        });

        $("img.lazy").lazyload({effect: "fadeIn"});
        //导出当前网站的询盘单
        $("span#export").on("click", function () {
            layer.confirm("请不要导出所有订单，确认后导出，时间会比较长", {title: "确认导出"}, function (index) {
                layer.close(index);
                var url = window.location.href;
                var newUrl = "/backend/sites/order/export";
                window.location.href = newUrl;
            });
        });
        requirejs.config({
            paths: {
                jquery: "../../components/jquery/jquery.min",
                viewer: "../../components/viewer/viewer.min",
            },
            shim: {
                viewer: {deps: ["loadcss!../../components/viewer/viewer.min.css"]},
            }
        });
        require(['jquery', 'viewer'], function ($) {
            $('.imgcol img').viewer({});
        });

        $(".resetorder").on("click", function () {
            $(this).parent().html('<span ondblclick="edit(this,\'shipping\');">双击手动录入单号</span>');
        });

    });


    $("span.shopping-btn").on("click", function () {
        itemid = $(this).attr("itemid");

        index = layer.open({
            type: 1,
            title: 'ID:' + itemid + '  采购信息',
            skin: 'layui-layer-rim',
            closeBtn: 1,
            area: ['50%', ''],
            shadeClose: true,
            content: '<div style="padding:20px 30px;"><div class="layui-layer-content" style="margin-bottom:10px;"><div class="form-group"><select name="shopfrom" class="form-control input-sm"><option value="">选择采购来源</option><option value="taobao">淘宝</option><option value="1688">1688</option><option value="aliexpress">速卖通</option><option value="other">其它</option></select></div><div class="form-group"><input type="text" name="ordersn" placeholder="输入采购单号" class="form-control input-sm "></div><div class="form-group"><input type="text" name="shipsn" placeholder="输入物流单号(选填)" class="form-control input-sm"><input type="hidden" name="orderid" value="' + itemid + '"/></div><div class="form-group"><button id="actshopping" class="btn btn-flat btn-info">确定提交</button></div></div></div>'
        });
    });

    $("body").on("click", "#actshopping", function () {
        var cid = $("input[name=orderid]").val();
        var source = $("select[name=shopfrom]").val();
        var ordersn = $("input[name=ordersn]").val();
        var shipsn = $("input[name=shipsn]").val();
        if (source.length == 0) {
            layer.msg("请选择采购来源");
            return;
        }
        if (ordersn.length == 0) {
            layer.msg("请输入采购订单号");
            return;
        }
        layer.load(1, {
            shade: [0.1, '#000'] //0.1透明度的白色背景
        });
        $.ajax({
            type: "post",
            url: "/backend/sites/order/ajaxshopping",
            dataType: "json",
            data: {"cid": cid, "source": source, "ordersn": ordersn, "shipsn": shipsn},
            error: function () {
                layer.closeAll();
            },
            success: function (result) {
                if (result["status"] == 1) {
                    window.location.href = window.location.href;
                } else {
                    layer.closeAll();
                    layer.msg(result["msg"]);
                }
            },
        });
    });


    var order_detail = function (title, oid) {
        layer.open({
            type: 2,
            title: title + ' 订单详情',
            shadeClose: true,
            shade: 0.8,
            area: ['94%', '90%'],
            content: '/backend/sites/order/detail/' + oid
        });
    }

    function edit(obj, attrName) {

        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class", "form-control input-sm");
        newobj.value = oldHtml;
        console.log(oldHtml);
        if (oldHtml == "双击添加备注" || oldHtml == "双击手动录入单号") {
            newobj.value = '';
        }
        var cid = $(obj).parent().attr("cid");
        obj.innerHTML = '';
        obj.appendChild(newobj);
        newobj.focus();

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function () {
            if (this.value == null || this.value == "") {
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value ? this.value : oldHtml;
            if (oldHtml != this.value) {
                $.ajax({
                    type: "post",
                    url: "/backend/sites/order/ajaxedit",
                    dataType: "json",
                    data: {"attrName": attrName, "newHtml": this.value, "cid": cid},
                    error: function () {
                    },
                    success: function (result) {
                        if (result["status"] != 1) {
                            layer.msg(result["msg"]);
                            obj.innerHTML = oldHtml;
                            return false;
                        }
                    },
                });
            }

        })
    }


    $("select#pagesize").change(function () {
        pagesize = $("select#pagesize option:selected").val();
        if (pagesize > 0) {
            var url = window.location.href;
            var newUrl = changeURLArg(url, "pagesize", pagesize);
            window.location.href = newUrl;
        }
    });


    function changeURLArg(url, arg, arg_val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + arg_val;
        if (url.match(pattern)) {
            var tmp = '/(' + arg + '=)([^&]*)/gi';
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match('[\?]')) {
                return url + '&' + replaceText;
            } else {
                return url + '?' + replaceText;
            }
        }
        return url + '\n' + arg + '\n' + arg_val;
    }
</script>
